package org.jeecg.glr.service.impl;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.glr.entity.*;
import org.jeecg.glr.mapper.*;
import org.jeecg.glr.service.IGlrLightPoleDeviceService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 路灯灯杆管绑定的设备
 * @Author: jeecg-boot
 * @Date: 2020-03-05
 * @Version: V1.0
 */
@Service
public class GlrLightPoleDeviceServiceImpl extends ServiceImpl<GlrLightPoleDeviceMapper, GlrLightPoleDevice> implements IGlrLightPoleDeviceService {

    @Resource
    GlrLightPoleDeviceMapper glrLightPoleDeviceMapper;

    @Resource
    GlrLightPoleMapper glrLightPoleMapper;

    @Resource
    GlrRadioMapper glrRadioMapper;

    @Resource
    GlrVideoMapper glrVideoMapper;

    @Resource
    GlrLedMapper glrLedMapper;

    @Resource
    GlrLightMapper glrLightMapper;

    @Resource
    GlrGatewayMapper glrGatewayMapper;

    @Resource
    GlrSensorMapper glrSensorMapper;

    @Resource
    private GlrVideoYsMapper glrVideoYsMapper;
    @Override
    public <T> void dataProecss(T entity,String type){
        String deviceType="0";
        String deviceId="";
        String lightPoleCode="";
        String projectCode="";
        String stFieldYn="";
        if (GlrRadio.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_01;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_01_RADIO_YN;
        }
        if (GlrLed.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_02;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_02_LED_YN;
        }
        if (GlrLight.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_03;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_03_LIGHT_YN;
        }
        if (GlrGateway.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_04;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_04_GATEWAY_YN;
        }
        if (GlrSensor.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_05;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_05_SENSOR_YN;
        }
        if (GlrVideo.class.equals(entity.getClass())){
            deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_06;
            stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_06_VIDEO_YN;
        }
        deviceId=ReflectUtil.invoke(entity, "getId");
        lightPoleCode=ReflectUtil.invoke(entity, "getLightPoleCode");
        projectCode=ReflectUtil.invoke(entity, "getProjectCode");

        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE.equals(type)){
            deleteEntity(deviceId,lightPoleCode,deviceType,projectCode,stFieldYn);
        }else{
            updateEntity(deviceId,lightPoleCode,deviceType,projectCode,stFieldYn);
        }
    }

    public void  updateEntity(String deviceId,String lightPoleCode,String deviceType,String projectCode,String stFieldYn){
        if (StringUtil.isNotEmpty(deviceId)&&StringUtil.isNotEmpty(lightPoleCode)&&StringUtil.isNotEmpty(deviceType)){
            QueryWrapper<GlrLightPoleDevice> queryWrapper = new QueryWrapper<GlrLightPoleDevice>();
            queryWrapper.eq("device_id", deviceId);
            queryWrapper.eq("device_type", deviceType);
            GlrLightPoleDevice glrLightPoleDevice=glrLightPoleDeviceMapper.selectOne(queryWrapper);
            if (glrLightPoleDevice==null){
                glrLightPoleDevice=new GlrLightPoleDevice();
                glrLightPoleDevice.setDeviceId(deviceId);
                glrLightPoleDevice.setDeviceType(deviceType);
                glrLightPoleDevice.setLightPoleCode(lightPoleCode);
                glrLightPoleDevice.setProjectCode(projectCode);
                glrLightPoleDeviceMapper.insert(glrLightPoleDevice);

                if (StringUtil.isNotEmpty(stFieldYn)){
                    glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_Y,lightPoleCode,projectCode);
                }
            }else{
                String findLightPoleCode=glrLightPoleDevice.getLightPoleCode();
                String findProjectCode=glrLightPoleDevice.getProjectCode();
                glrLightPoleDevice.setProjectCode(projectCode);
                glrLightPoleDevice.setLightPoleCode(lightPoleCode);
                glrLightPoleDeviceMapper.updateById(glrLightPoleDevice);

                if (StringUtil.isNotEmpty(stFieldYn)){
                    List<GlrLightPoleDevice> glrLightPoleDeviceList=glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListByFind(findLightPoleCode,findProjectCode,deviceType,null);
                    if (glrLightPoleDeviceList.size()>0){
                        glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_Y,findLightPoleCode,findProjectCode);
                    }else {
                        glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_N,findLightPoleCode,findProjectCode);
                    }
                    glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_Y,lightPoleCode,projectCode);
                }
            }
        }
    }

    public void  deleteEntity(String deviceId,String lightPoleCode,String deviceType,String projectCode,String stFieldYn){
        if (StringUtil.isNotEmpty(deviceId)&&StringUtil.isNotEmpty(lightPoleCode)&&StringUtil.isNotEmpty(deviceType)){

            List<GlrLightPoleDevice> glrLightPoleDeviceList=glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListByFind(lightPoleCode,projectCode,deviceType,deviceId);
            if (glrLightPoleDeviceList.size()>0){
                glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_Y,lightPoleCode,projectCode);
            }else {
                glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_N,lightPoleCode,projectCode);
            }
            glrLightPoleDeviceMapper.deleteByDeviceIdAndDeviceType(deviceId,deviceType);
        }
    }

    @Override
    public void bingLightPoleDevice(GlrLightPoleDevice domain){
        if(StringUtil.isNotEmpty(domain.getIds())){
            String [] ids=domain.getIds().split(",");
            for(int i=0;i<ids.length;i++){
                if(StringUtil.isNotEmpty(ids[i])){
                    String deviceId=ids[i];
                    String lightPoleCode=domain.getLightPoleCode();
                    String deviceType=domain.getDeviceType();
                    String projectCode=domain.getProjectCode();
                    String stFieldYn="LIGHT_YN";
                    updateEntity(deviceId,lightPoleCode,deviceType,projectCode,stFieldYn);
                }
            }
        }
    }

    @Override
    public List<GlrLightPoleDevice> glrLightPoleDeviceListById(String light_pole_code,String projectCode) {
        return glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListById(light_pole_code,projectCode);
    }
    @Override
    public List<GlrRadio> getRadioByPoleCodeAndProjectCode(String code, String projectCode) {
        glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListById(code,projectCode);
        return null;
    }

    @Override
    public void removeEntityById(String id){
        GlrLightPoleDevice glrLightPoleDevice=glrLightPoleDeviceMapper.selectById(id);
        deleteEntityProecss(glrLightPoleDevice);
        glrLightPoleDeviceMapper.deleteById(id);
    }


    @Override
    public void deleteByGateWayCode(String gateWayCode){

        List<GlrLightPoleDevice> glrLightPoleDeviceListe=glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListByGateWayCode(gateWayCode);

        glrLightPoleDeviceMapper.deleteByGateWayCode(gateWayCode);

        String stFieldYn=GLRConstant.GLR_LIGHT_POLE_DEVICE_03_LIGHT_YN;
        String deviceType=GLRConstant.GLR_LIGHT_POLE_DEVICE_03;
        for(GlrLightPoleDevice glrLightPoleDevice:glrLightPoleDeviceListe){
            String projectCode=glrLightPoleDevice.getProjectCode();
            String lightPoleCode=glrLightPoleDevice.getLightPoleCode();
            List<GlrLightPoleDevice> glrLightPoleDeviceList=glrLightPoleDeviceMapper.selectGlrLightPoleDeviceListByFind(lightPoleCode,projectCode,deviceType,"");
            if (glrLightPoleDeviceList.size()>0){
                glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_Y,lightPoleCode,projectCode);
            }else {
                glrLightPoleMapper.updateFieldYnByCodeAndProjectCode(stFieldYn,GLRConstant.YN_N,lightPoleCode,projectCode);
            }
        }
    }
    @Override
    public void removeEntityByIds(List<String> asList){
        for(String stId:asList){
            GlrLightPoleDevice glrLightPoleDevice=glrLightPoleDeviceMapper.selectById(stId);
            deleteEntityProecss(glrLightPoleDevice);
            glrLightPoleDeviceMapper.deleteById(stId);
        }
    }

    private void deleteEntityProecss(GlrLightPoleDevice glrLightPoleDevice) {
        String updateTable="";
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_01.equals(glrLightPoleDevice.getDeviceType())){
            GlrRadio glrRadio=glrRadioMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrRadio==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else {
                dataProecss(glrRadio, GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable = GLRConstant.GLR_LIGHT_POLE_DEVICE_01_TABLE;
            }
        }
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_02.equals(glrLightPoleDevice.getDeviceType())){
            GlrLed glrLed=glrLedMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrLed==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else {
                dataProecss(glrLed, GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable = GLRConstant.GLR_LIGHT_POLE_DEVICE_02_TABLE;
            }
        }
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_03.equals(glrLightPoleDevice.getDeviceType())){
            GlrLight glrLight=glrLightMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrLight==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else{
                dataProecss(glrLight,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable=GLRConstant.GLR_LIGHT_POLE_DEVICE_03_TABLE;
            }
        }
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_04.equals(glrLightPoleDevice.getDeviceType())){
            GlrGateway glrGateway=glrGatewayMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrGateway==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else {
                dataProecss(glrGateway, GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable = GLRConstant.GLR_LIGHT_POLE_DEVICE_04_TABLE;
            }
        }
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_05.equals(glrLightPoleDevice.getDeviceType())){
            GlrSensor glrSensor=glrSensorMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrSensor==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else {
                dataProecss(glrSensor, GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable = GLRConstant.GLR_LIGHT_POLE_DEVICE_05_TABLE;
            }
        }
        if (GLRConstant.GLR_LIGHT_POLE_DEVICE_06.equals(glrLightPoleDevice.getDeviceType())){
            GlrVideo glrVideo=glrVideoMapper.selectById(glrLightPoleDevice.getDeviceId());
            if (glrVideo==null){
                glrLightPoleDeviceMapper.deleteById(glrLightPoleDevice.getId());
            }else {
                dataProecss(glrVideo, GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
                updateTable = GLRConstant.GLR_LIGHT_POLE_DEVICE_06_TABLE;
            }
        }

        if (StringUtil.isNotEmpty(updateTable)){
            glrLightPoleDeviceMapper.updateTableLightPoleCodeById(updateTable,glrLightPoleDevice.getDeviceId());
        }
    }

}
